<?php 
class Places_model extends Model{

	function __construct (){
		parent::__construct();
	}

	public function insert_place($PlaceName,$address,$steet,$city,$Description){

		$error 	 = array();
		$last_id = 0;
		
		try{

			$q = $this->db->insert('place',array(
				'name' 		=> 	$PlaceName,
				'address'		=>	$address,
				'steet' 		=> 	$steet,
				'city'			=>	$city,
				'Description' 	=> 	$Description,
			));

			$last_id = $this->db->lastInsertId() ;
		}catch(Exception $e){
			$last_id = 'false';
		}
		return $last_id;
	}

	public function insert_wifi($place_id,$wifiName,$password,$pin){

		$q = $this->db->insert('place',array(
			'place_id' 		=> 	$place_id,
			'name'			=>	$wifiName,
			'password' 		=> 	$password,
			'pin'			=>	$pin
		));
		return $q;
	}

	public function get_all_place(){
		$q = $this->db->prepare("SELECT * FROM place ORDER BY name ");
		$q->execute();
		$data = false;
		try {
			$data = $q->fetchAll();
		} catch (Exception $e) {
			$data = false;
		}
		return $data;
	}

	public function get_all_wifi($place_id){
		$q = $this->db->prepare("SELECT id,name,password,votes,(votes+unvotes) as total,
			CASE 
				WHEN ( votes + unvotes ) > 0 THEN ROUND( ( votes / ( votes + unvotes ) ) * 100 )
			ELSE 0 END as rate ,pin,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(YEAR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MONTH,last_date,NOW())
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(DAY,last_date,NOW())
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(HOUR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MINUTE,last_date,NOW())
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(SECOND,last_date,NOW())
				ELSE ' ' END AS last_date,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN ' year ago'
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN ' month ago'
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN ' day ago'
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN ' hour ago'
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN ' minute ago'
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) > 0 
					THEN ' second ago'
				ELSE '' END AS title_last_date
			 FROM wifi WHERE place_id=:place_id ORDER BY name ");

		$q->execute(array(
			':place_id' => $place_id 
		));

		$data = false;
		
		try {
			$data = $q->fetchAll();
		} catch (Exception $e) {
			$data = false;
		}
		return $data;
	}

	public function get_curen_wifi($wifi_id){
		$q = $this->db->prepare("SELECT id,name,password,votes,(votes+unvotes) as total,
			CASE 
				WHEN ( votes + unvotes ) > 0 THEN ROUND( ( votes / ( votes + unvotes ) ) * 100 )
			ELSE 0 END as rate ,pin,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(YEAR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MONTH,last_date,NOW())
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(DAY,last_date,NOW())
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(HOUR,last_date,NOW())
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN TIMESTAMPDIFF(MINUTE,last_date,NOW())
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) >= 0 
					THEN TIMESTAMPDIFF(SECOND,last_date,NOW())
				ELSE ' ' END AS last_date,
			CASE 
				WHEN TIMESTAMPDIFF(YEAR,last_date,NOW()) > 0 
					THEN ' year ago'
				WHEN TIMESTAMPDIFF(MONTH,last_date,NOW()) > 0 
					THEN ' month ago'
				WHEN TIMESTAMPDIFF(DAY,last_date,NOW()) > 0 
					THEN ' day ago'
				WHEN TIMESTAMPDIFF(HOUR,last_date,NOW()) > 0 
					THEN ' hour ago'
				WHEN TIMESTAMPDIFF(MINUTE,last_date,NOW()) > 0 
					THEN ' minute ago'
				WHEN TIMESTAMPDIFF(SECOND,last_date,NOW()) >= 0 
					THEN ' second ago'
				ELSE '' END AS title_last_date
			 FROM wifi WHERE id=:wifi_id ORDER BY name ");

		$q->execute(array(
			':wifi_id' => $wifi_id 
		));

		$data = false;
		
		try {
			$data = $q->fetchAll();
		} catch (Exception $e) {
			$data = false;
		}
		return $data;
	}

	public function update_vote_wifi($id_wifi,$is_vote)
	{
		$sql = "";
	    if($is_vote)
	    {
	    	$sql = "UPDATE wifi SET votes = votes + 1, last_date = NOW() WHERE id = :id ";
	    }else {
	    	$sql = "UPDATE wifi SET unvotes = unvotes + 1, last_date = NOW() WHERE id = :id ";
	    }
	    $q = $this->db->prepare($sql);

	    $value = $q->execute(array(
	    	'id'	=>	$id_wifi
	    ));
	    return $value;
	}

}

